﻿@model IEnumerable<MAPDavy.Entities.MatriculaDTO>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section JavaScript{
    <script type="text/javascript">
        $(document).ready(function () {

            $('.desc').width('450px');

            $("#tblAlumnos tbody tr").hover(
                function () {
                    $(this).addClass("ui-state-highlight");
                },
                function () {
                    $(this).removeClass("ui-state-highlight");
                });

            $("#grade").change(function () {
                LoadSections($("#grade").val());
            });

            $(".deleteLink").click(function (e) {
                e.preventDefault();
                var targetUrl = $(this).attr("href");

                var tr = $(this).parents("tr");
                var matId = tr.find("input:hidden").val();

                jConfirm('Desea Eliminar el Item Seleccionado', 'Confirmación', function (r) {
                    if (r) {
                        $.post(
                                   '@Url.Action("CanDeleteServiceJson", "Alumnos")',
                                    { id: matId },
                                    function (can) {
                                        if (can) {
                                            $.post(
                                                    '@Url.Action("Delete", "Alumnos")',
                                                    { id: matId },
                                                    function (ok) {
                                                        if (ok) {
                                                            $(tr).fadeOut("slow", function () {
                                                                $(tr).remove();
                                                            });
                                                        }
                                                        else {
                                                            jAlert("No se ha puede Eliminar el Item Seleccionado", "Advertencia");
                                                        }
                                                    },
                                                    "json"
                                               );
                                        }
                                        else {
                                            jAlert("No se puede Eliminar el Item Seleccionado porque tiene información Asociada", "Advertencia");
                                        }
                                    },
                                    "json"
                            );
                    }
                });
            });
        });

        function LoadSections(id) {
            var dd = document.getElementById("seccion");
            dd.options.length = 0;
            dd.options[0] = new Option("Espere...");
            dd.selectedIndex = 0;
            dd.disabled = true;

            // Control de errores

            $("#seccion").ajaxError(function (event, request, settings) {
                dd.options[0] = new Option("Selecciona un Grado");
            });

            // Obtenemos los datos...

            $.getJSON(
                    '@Url.Action("SeccionServiceJson", "Seccion")', // URL a la acción "../Seccion/SeccionServiceJson"
                    {gradeId: id },                  // Objeto JSON con parámetros
                    function (data) {                    // Función de retorno exitoso
                        $.each(data, function (i, item) {
                            dd.options[i] = new Option(item.Seccion, item.Id);
                        });
                        dd.disabled = false;
                    });        }
    </script>
}
<div class="cpanel ui-widget ui-widget-content ui-corner-all">
    <table class="toolbar">
        <tr>
            <td valign="middle" >
                <img src="@Url.Content("~/Content/images/alumno.png")" title="Alumnos" alt="Alumnos" />
            </td>
            <td class="tdHeader"><span class="header">Alumnos Matriculados</span></td>
            @if (Request.IsAuthenticated && @User.IsInRole("admin"))
            {
                <td class="cerrado"><center>
                    <a href="@Url.Action("Create", "Alumnos")">
                        <img src="@Url.Content("~/Content/images/newAlumnoT.png")" title="Matricular Alumno" alt="Matricular Alumno" />
                        <br /><span class="label">Nuevo</span>
                    </a></center>
                </td>
            }
        </tr>
    </table>
</div>
@using (Html.BeginForm(null, null, FormMethod.Post))
{ 
    <div id="titleView" class="ui-widget ui-widget-content ui-corner-all" >
        <b> @Html.Label("Criterio", "Criterio: ")</b> @Html.TextBox("criterio", null, new { @style = "width: 400px;" }) 
        <b> Grade Level: </b> @Html.DropDownList("grade") 
        <b> Class Name: </b> @Html.DropDownList("seccion") 
        <input id="buscar" type="submit" value="Buscar" />
    </div>
}

<table id="tblAlumnos">
    <tr>
        <th class="ui-widget ui-widget-header">
            Student Id
        </th>
        <th class="ui-widget ui-widget-header desc">
            Student
        </th>
        <th class="ui-widget ui-widget-header">
            Grade Level
        </th>
        <th class="ui-widget ui-widget-header">
            Class Name
        </th>
        <th class="ui-widget ui-widget-header">
            Fecha de Ingreso
        </th>
        <th class="ui-widget ui-widget-header">
            Activo
        </th>
        <th class="ui-widget ui-widget-header">Acciones</th>
    </tr>
    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.Hidden("id." + item.Id, item.Id)
            @Html.DisplayFor(modelItem => item.StudentId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Student)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GradeLevel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ClassName)
        </td>
        <td>
            @if (item.FechaIngreso.HasValue)
            {
		        <center>@item.FechaIngreso.Value.ToShortDateString()</center>
            }
        </td>
        <td>
            <center>@Html.DisplayFor(modelItem => item.Activo)</center>
        </td>
        <td>
            <center>
                <a href="@Url.Action("Edit", "Alumnos", new { id = item.Id })">
                    <img src="@Url.Content("~/Content/images/edit.png")" title="Editar Matricula" alt="Editar Matricula" /></a> 
                @if (Request.IsAuthenticated && @User.IsInRole("admin"))
                {
                <a href="@Url.Action("Delete", "Alumnos", new { id = item.Id })" class = "deleteLink cerrado">
                    <img src="@Url.Content("~/Content/images/delete.png")" title="Eliminar Matricula" alt="Eliminar Matricula" /></a>  
                }
            </center>      
        </td>
    </tr>
}
</table>
